package com.gao.modules.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gao.base.Result;
import com.gao.modules.role.mapper.SysRoleMapper;
import com.gao.modules.role.pojo.SysRole;
import com.gao.modules.role.service.SysRoleService;
import com.gao.modules.role.vo.SysRoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public IPage<SysRoleVo> queryAllRole(Page page) {
        return sysRoleMapper.queryAllRole(page);
    }

    @Override
    @Transactional
    public Result insertRole(SysRole sysRole) {
        SysRole existRole = this.getOne(new QueryWrapper<SysRole>().eq("name", sysRole));
        if(null != existRole){
            return Result.error("已有该角色姓名，请重新输入");
        }else {
            sysRoleMapper.insert(sysRole);
            return Result.success();
        }
    }

    @Override
    @Transactional
    public Result updateRole(SysRole sysRole) {
        boolean b = this.updateById(sysRole);
        if (b){
            return Result.success();
        }else
            return Result.error("更新失败，请重新尝试");
    }

    @Override
    @Transactional
    public Result deleteRole(String roleId) {
        boolean b = this.removeById(roleId);
        if (b){
            return Result.error("删除失败，请重新尝试");
        }else{
            return Result.success();
        }
    }

}
